package com.babyqiao.wallpaper.portal.service.impl;

import com.babyqiao.wallpaper.portal.dao.HomeContentMapper;
import com.babyqiao.wallpaper.portal.entity.WallpaperAlbum;
import com.babyqiao.wallpaper.portal.entity.WallpaperCmsSwiper;
import com.babyqiao.wallpaper.portal.entity.vo.AlbumAndTopVO;
import com.babyqiao.wallpaper.portal.entity.vo.AlbumResVO;
import com.babyqiao.wallpaper.portal.entity.vo.HomeContentVO;
import com.babyqiao.wallpaper.portal.service.IWallpaperAlbumService;
import com.babyqiao.wallpaper.portal.service.IWallpaperCmsSwiperService;
import com.babyqiao.wallpaper.portal.service.IhomeContentService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author QLZ
 * @date 2021/2/24
 */
@Service
public class HomeContentServiceImpl implements IhomeContentService {
    @Autowired
    IWallpaperCmsSwiperService swiperService;
    @Autowired
    HomeContentMapper homeContentMapper;
    @Autowired
    IWallpaperAlbumService albumService;
    @Autowired
    RedissonClient redissonClient;

    /**
     * 获取首页数据
     *
     * @Author QLZ
     * @Date 2021/2/24
     * @return: com.babyqiao.wallpaper.portal.entity.vo.HomeContentVO
     */
    @Cacheable(value = "HomeContent", key = "'HomeContent'", sync = true)
    @Override
    public HomeContentVO getContentService() {
        //添加分布式锁
        RLock lock = redissonClient.getLock("home-lock");
        lock.lock();
        HomeContentVO homeContentVO;
        try {
            homeContentVO = getHomeContentVO();
            return homeContentVO;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
        return null;
    }

    private HomeContentVO getHomeContentVO() {
        System.out.println("查询数据库。。。。。。。");
        HomeContentVO homeContentVO = new HomeContentVO();
        //设置轮播图
        homeContentVO.setSwipers(getSwiper());
        //设置最热专辑
        homeContentVO.setHotAlbums(getHotAlbum());
        //设置人气专辑
        homeContentVO.setLikeAlbums(getLikeAlbum());
        //设置最新专辑
        homeContentVO.setNewAlbums(getNewAlbum());
        return homeContentVO;
    }




    private List<WallpaperCmsSwiper> getSwiper() {
        QueryWrapper<WallpaperCmsSwiper> queryWrapper = new QueryWrapper<>();
        queryWrapper.select().orderByAsc("sort").last("limit 5");
        return swiperService.list(queryWrapper);

    }

    private List<AlbumResVO> getHotAlbum() {
        return homeContentMapper.selectHotAlbum();
    }

    private List<AlbumResVO> getLikeAlbum() {
        return homeContentMapper.selectLikeAlbum();
    }

    private List<AlbumResVO> getNewAlbum() {
        return homeContentMapper.selectNewAlbum();

//        QueryWrapper<WallpaperAlbum> queryWrapper = new QueryWrapper<>();
//        queryWrapper.select().eq("online",1).orderByDesc("gmt_modified").last("limit 6");
//        return albumService.list(queryWrapper);
    }


}
